package com.zf.shuashua.advert;

import android.app.Activity;

import com.dydroid.ads.c.ADError;
import com.dydroid.ads.c.ADLoader;
import com.dydroid.ads.c.video.RewardVideoADListener;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.AdvertUtils;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.AdvertReport;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.manager.ActionThirdReportManager;
import com.zf.shuashua.utils.Util;

import java.lang.ref.WeakReference;

/**
 * 激励视频展示
 * 旺翔激励视频
 */
public class RewardAdvertWxManager {

    private com.dydroid.ads.c.ADLoader adLoader;
    private String mAdvertType = AdvertConstants.REWARD_TYPE_TASK;
    private String mCodeId = "D1050085";
    private String mReportID = "";

    private OnRewardListener mListener;

    private static RewardAdvertWxManager instance;
    private WeakReference<Activity> weakReference;
    private boolean isComplete = false;

    private RewardAdvertWxManager() {
    }

    public static RewardAdvertWxManager getInstance() {
        if (null == instance) {
            synchronized (RewardAdvertWxManager.class) {
                if (null == instance) {
                    instance = new RewardAdvertWxManager();
                }
            }
        }
        return instance;
    }

    public void showAdvert(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        this.weakReference = new WeakReference<>(activity);
        this.mAdvertType = advertType;
        this.mReportID = reportId;
        this.mListener = listener;
        isComplete = false;
        mCodeId = getCodeId();
        loadAd();
    }

    private void reportRequestStart(){
        ActionThirdReportManager.getInstance().reportRequestStart(9, 2, mCodeId, 1);
    }

    private void reportRequestSucc(int return_num) {
        ActionThirdReportManager.getInstance().reportRequestSucc(9, 2, mCodeId, return_num);
    }

    private void reportRequestFail(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestFail(9, 2, mCodeId, 1, error_code, error_msg);
    }

    private void reportRequestRend(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestRend(9, 2, mCodeId, error_code, error_msg);
    }

    private void dealClose(String planId, OnRewardListener listener){
        if(!isComplete){
            return;
        }
        AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoEnd, getReportType(), "9", new ACallback<BaseData>() {
            @Override
            public void onSuccess(BaseData data) {
                if (data != null && data.isStatus()) {
                    weakReference.get().runOnUiThread(() -> {
                        if (listener != null) {
                            listener.onAdClose();
                        }
                    });
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }

    private String uuiThirdAdvert;
    private String planId;

    private void loadAd() {
        uuiThirdAdvert = Util.getUUID32();
        String userId = Store.getInstance().getuserId();
        adLoader = new ADLoader.Builder(weakReference.get())
                .setCodeId(mCodeId)
                .setRewardName("金豆")
                .setRewardAmount(100)
                .setUserID(userId)
                .setVolumnOn(false)
                .build();

        planId = System.identityHashCode(adLoader) + "";

        adLoader.loadAndShowRewardVideoAd(new RewardVideoADListener() {

            @Override
            public void onADError(ADError adError) {
                weakReference.get().runOnUiThread(() -> {
                    if (mListener != null) {
                        mListener.onVideoError();
                    }
                });
                if(adError != null){
                    reportRequestFail(adError.getErrorCode() + "", adError.getErrorMessage());
                }
            }

            @Override
            public void onAdLoaded() {
                reportRequestSucc(1);
            }

            @Override
            public void onAdReward() {
                isComplete = true;
            }

            @Override
            public void onAdClicked() {
                if (mListener != null) {
                    mListener.onAdClick();
                }
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoClick, getReportType(), "9");
            }

            @Override
            public void onAdShow() {
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoStart, getReportType(), "9");
            }

            @Override
            public void onAdVideoCompleted() {
                isComplete = true;
                if (mListener != null) {
                    mListener.onVideoComplete();
                }
            }

            @Override
            public void onAdExposure() {
            }

            @Override
            public void onAdDismissed() {
                dealClose(planId, mListener);
            }
        });

        reportRequestStart();
    }

    private String getReportType() {
        return AdvertUtils.getReportType(mAdvertType, mReportID);
    }

    private String getCodeId() {
        return "D1050085";
    }

}
